package com.intct.hbase.dml;

import com.intct.hbase.util.HBaseConnection;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-07-11 10:44
 */
public abstract class AbsDMLBase {
    public void start(String namespace, String tableName) throws IOException {
        // 1 连接HBase
        Connection connection = HBaseConnection.getConnection();

        // 2 创建TableName实例
        TableName hbaseTableName = TableName.valueOf(namespace, tableName);

        // 3 创建Admin
        Admin admin = connection.getAdmin();

        // 4 表存在，则插入数据
        if (admin.tableExists(hbaseTableName)) {
            // 5 创建Table
            Table table = connection.getTable(hbaseTableName);
            try {
                // 6 具体执行命令(抽象方法，需要继承者实现)
                execCommand(table);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭资源
                if (table != null) {
                    table.close();
                }
            }

        } else {
            System.out.println("表不存在，无法插入数据！");
        }

        if (admin != null) {
            admin.close();
        }
//        RegionSplitPolicy
    }

    public abstract void execCommand(Table table) throws IOException;
}
